package com.ganzalang.gmall.interfacespecification.first.service.impl;

import com.ganzalang.gmall.interfacespecification.first.domain.User;
import com.ganzalang.gmall.interfacespecification.first.service.UserService;
import org.springframework.util.StringUtils;

import java.util.regex.Pattern;

/**
 * @description:
 * @author: Ganzalang
 * @Date 2021/3/30 9:23
 */
public class UserServiceImpl implements UserService {

    /**
     * 添加用户，先做校验，再做业务逻辑
     * @param user
     * @return
     */
    @Override
    public String addUser(User user) {
        if (user == null || user.getId() == null || user.getAccount() == null || user.getPassword() == null || user.getEmail() == null) {
            return "对象或对象字段不能为空";
        }
        if (StringUtils.isEmpty(user.getAccount()) || StringUtils.isEmpty(user.getPassword()) || StringUtils.isEmpty(user.getEmail())) {
            return "不能输入空字串";
        }
        if (user.getAccount().length() < 6 || user.getAccount().length() > 11) {
            return "账号长度必须是6-11个字符";
        }
        if (user.getPassword().length() < 6 || user.getPassword().length() > 16) {
            return "密码长度必须是6-16个字符";
        }
        if (!Pattern.matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$", user.getEmail())) {
            return "邮箱格式不正确";
        }
        // 参数校验完毕后这里做业务逻辑
        return "success";
    }
}
